package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.bean.ShoppingCart;
import com.itheima.dao.ShoppingCartDao;
import com.itheima.service.ShoppingCartService;
import com.itheima.utils.MyBaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Transactional
public class ShoppingCartServiceImpl implements ShoppingCartService {

    //注入dao
    @Autowired
    private ShoppingCartDao shoppingCartDao;

    @Override
    public ShoppingCart add(ShoppingCart shoppingCart) {
        //判断是套餐的添加请求还是菜品的添加请求，如果菜品id有值则表示是菜品的添加请求，反之是套餐的添加购物车请求

        //创建条件对象
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        //添加查询条件
        qw.eq(ShoppingCart::getUserId, MyBaseContext.getThreadLong());
        if (shoppingCart.getDishId() != null) {
            qw.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        } else {
            //添加查询条件
            qw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        //调用dao进行查询，返回购物车中的菜品对象
        ShoppingCart s = shoppingCartDao.selectOne(qw);
        //判断该用户是否是第一次添加该菜品，如果上面查询对象为nul则是第一次，否则就是第二次

        if (s == null) {
            //往里面补充对应的数据
            shoppingCart.setUserId(MyBaseContext.getThreadLong());
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);
            //调用dao进行添加操做
            int row = shoppingCartDao.insert(shoppingCart);

            //更具row变量判断是否添加成功，row大于0则添加成功，反之添加失败

            //进入这里表示添加成功，将添加后的购物车中的菜品对象返回给Controller层
            return shoppingCart;
        } else {
            //进入这里表示已经有添加过该菜品，将数量数据+1就可以了
            s.setNumber(s.getNumber() + 1);
            //在调用dao进行再一次添加
            int row = shoppingCartDao.updateById(s);
            //判断是否添加成功，成功则返回添加后的购物车菜品对象

            return s;

        }
        //判断查询返回的对象是否为空，为空则表示第一次填该套餐
            /*if (s==null){
                //往里面补充对应的数据
                shoppingCart.setUserId(MyBaseContext.getThreadLong());
                shoppingCart.setCreateTime(LocalDateTime.now());
                shoppingCart.setNumber(1);
                //调用dao进行添加操做
                int row = shoppingCartDao.insert(shoppingCart);

                //更具row变量判断是否添加成功，row大于0则添加成功，反之添加失败
                *//*if (row > 0) {
                    //进入这里表示添加成功，将添加后的购物车中的套餐对象返回给Controller层
                    return shoppingCart;
                }*//*
            }else{
                //进入这里表示已经有添加过该套餐，将数量数据+1就可以了
                s.setNumber(s.getNumber() + 1);
                //在调用dao进行再一次添加
                int row = shoppingCartDao.updateById(s);
                //判断是否添加成功，成功则返回添加后的购物车套餐对象
                *//*if (row > 0) {

                    return s;
                }*//*
            }*/


        /*return shoppingCart;*/
    }

    @Override
    public List<ShoppingCart> updateList() {
        //查询购物车中的所有购买数据，返回给前端页面

        return shoppingCartDao.selectList(null);
    }

    @Override
    public List<ShoppingCart> findByUId(Long id) {
        //创建条件查询对象
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId, id);
        return shoppingCartDao.selectList(qw);
    }

    @Override
    public int delete(Long id) {
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId, id);
        return shoppingCartDao.delete(qw);
    }

    @Override
    public ShoppingCart update(Long dishId) {
            //根据id进行查询操作，判断数量number的值是否大于1，大于1number的值减一
            LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
            //添加查询条件
            qw.eq(ShoppingCart::getDishId, dishId);
            //先进行查询
            ShoppingCart shoppingCart = shoppingCartDao.selectOne(qw);
            if (shoppingCart.getNumber() > 1) {
                shoppingCart.setNumber(shoppingCart.getNumber() - 1);
                int row = shoppingCartDao.updateById(shoppingCart);
                return shoppingCart;
            }else {
                shoppingCartDao.delete(qw);
                return null;
            }


    }
}
